Passing output of SQLAlchemy from views.py to html (to be used eventually in javascript file ) in Django

by: ankur25, 8 years ago


I'm trying to pass the output of SQLAlchemy from views.py to html which is further passed to a javascript file.
In the javascript file, i need it in csv format for further processing.
The output format of SQLAlchemy is in the llist of tuple format:
example:  [(a,b),(c,d),(e,f),(g,h)] for a table with 2 columns , where (a,b) is a row of the table.

When i tried to pass it using HttpResponse it is getting converted to text format.

Any help will be appreciated. Thanks!!



You must be logged in to post. Please login or register an account.



Hmm, I am not sure why you must have it in csv format. Typically javascript is going to want json format at the end of the day, and you can pass json straight from python to javascript.

-Harrison 8 years ago

You must be logged in to post. Please login or register an account.


Thanks Harrison this was really helpful.
I got my data in json format using json.dumps. But the only problem is jason.dumps seems to jumble up the order of the columns. I tries sort_keys, but it sorts the columns in alpha numeric order.For my project i need
to have them in same order as i pass so that they can be published on the webpage as a table.

Thanks in advance!!

-ankur25 8 years ago

You must be logged in to post. Please login or register an account.